import orm


# from orm import read_data,write_data

def show_products():
    sql = f"""
        select num,name,price,discount from products
    """
    data = orm.read_data(sql)
    if data != ():
        print(f'----编码----名称----价格----折扣----')
        for product in data:
            print(f'{product[0]}\t{product[1]}\t{product[2]}\t{product[3]}')


def get_product():
    num = input('请输入要查询的商品编码：')
    sql = f"""
        select num,name,price,discount from products where num='{num}'
    """
    data = orm.read_data(sql)
    if data != ():
        print(f'----编码----名称----价格----折扣----')
        for product in data:
            print(f'{product[0]}\t{product[1]}\t{product[2]}\t{product[3]}')
    else:
        print('商品不存在，请重新输入')
    return data


def add_product():
    pass


def mod_product():
    pass


def del_product():
    pass


def show_orders():
    pass


def del_order():
    pass


def count_orders():
    pass


def add_order():
    amount = 0
    count = 0
    while True:
        data = get_product()
        if data != ():
            num = int(input('请输入要购买的数量：'))
            money = data[0][2] * num
            count += num
            amount += money
            choice = int(input('请输入你的选择1继续2退出：'))
            if choice == 1:
                continue
            else:
                break
    print(f'总数量{count}总金额{amount}')
    sql = f"""
        select max(num) from orders
    """
    data = orm.read_data(sql)
    id = data[0][0] + 1
    sql = f"""
        insert into orders values(null,{id},{count},{amount})
    """
    rows = orm.write_data(sql)
    if rows > 0:
        print('订单写入成功')
    else:
        print('订单写入失败')


if __name__ == '__main__':
    # 测试一下
    # show_products()
    # get_product()
    add_order()